#include <stdio.h>
#include <string.h>
#define MAXP 20000000
#define M 1000000007

int f[MAXP],o[MAXP];
int p,n,i,ans;
long long h;

int main()
{
	int ts;
	scanf("%d%d%d", &n, &p, &ts);
	f[0]=1;
	f[1]=n;
	o[1]=1;
	for(i=2;i<p;i++)
	{
		o[i]=(long long)(M-M/i)*o[M%i]%M;
		f[i]=(long long)f[i-1]*(n-1+i)%M*o[i]%M;
	}
	while(ts--)
	{
		scanf("%lld",&h);
		ans=1;
		while(h)
		{
			ans=(long long)ans*f[h%p]%M;
			h/=p;
		}
		printf("%d\n",ans);
	}
	return 0;
}